package com.bitdf.txing.product.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bitdf.txing.common.utils.PageUtils;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.product.entity.AttrEntity;
import com.bitdf.txing.product.entity.AttrGroupEntity;
import com.bitdf.txing.product.service.AttrAttrgroupRelationService;
import com.bitdf.txing.product.service.AttrGroupService;
import com.bitdf.txing.product.service.AttrService;
import com.bitdf.txing.product.service.CategoryService;
import com.bitdf.txing.product.vo.AttrGroupRelationVo;
import com.bitdf.txing.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author lizhiwei
 * @email sunlightcs@gmail.com
 * @date 2022-05-16 20:22:24
 */
@RestController
@RequestMapping("${txing.request.path.product}/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 添加 属性组&属性 关联
     * @param vos
     * @return
     */
    @SaCheckLogin
    @SaCheckPermission("mall:product:attr:relate:add")
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos)
    {
        attrAttrgroupRelationService.saveBatch(vos);
         return R.ok();
    }

    /**
     * 查询属性组的属性
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId)
    {
         List<AttrEntity> entitys=attrService.getRelationAttr(attrgroupId);
         return R.ok().put("data", entitys);
    }


    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String,Object> params)
    {
        PageUtils page=attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 删除属性关联
     * @param vos
     * @return
     */
    @SaCheckLogin
    @SaCheckPermission("mall:product:attr:relate:delete")
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }

//    @Autowired
//    AttrService attrService;
//    @GetMapping("/base/list/{catelogId}")
//    public R baseAttrList(@RequestParam Map<String,Object> params,@PathVariable("catelogId") Long catelogId){
//        PageUtils page=attrService.queryBaseAttrPage(params,catelogId, type);
//        return R.ok().put("page", page);
//    }
    /**
     * 查找指定分类下的属性数组
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId")Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path=categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @SaCheckLogin
    @SaCheckPermission("mall:product:attr:group:add")
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @SaCheckLogin
    @SaCheckPermission("mall:product:attr:group:update")
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @SaCheckLogin
    @SaCheckPermission("mall:product:attr:group:delete")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 查询分类下的数属性组（包括属性组下的属性也一块封装返回）
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId)
    {
        List<AttrGroupWithAttrsVo> vos=attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", vos);
    }

}
